﻿using Dapper;
using Infrastructure.DataBaseConnect;
using Infrastructure.IRepository;
using Model.BusinessModel.InPut;
using Model.DomainModel;
using Utility.Enum;

namespace Infrastructure.Repository
{
    /// <summary>
    /// 表单仓储接口实现
    /// </summary>
    public class FormRepository : Repository<Form>, IFormRepository
    {
        /// <summary>
        /// 根据表单名称获取数据
        /// </summary>
        /// <param name="formName">表单名称</param>
        /// <returns></returns>
        public Form? GetFormByFormName(string formName)
        {
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            string sql = "  select  * from Form where FormName  = @FormName ";
            return connection.QueryFirstOrDefault<Form>(sql, new { FormName = formName });
        }

        /// <summary>
        /// 更新数据状态
        /// </summary>
        /// <param name="input">输入模型</param>
        /// <returns></returns>
        public void UpdateFormDataState(UpdateDataStateInput input)
        {
            string sql = "   update Form set DataState=@DataState where FormId in @FormId  ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Query(sql, new { FormId = input.Ids, input.DataState });
        }

        /// <summary>
        /// 根据部门ID,获取表单
        /// </summary>
        /// <param name="departmentId">部门id</param>
        /// <returns></returns>
        public List<Form> GetFormByDepId(string departmentId)
        {
            string sql = " select FormId,FormName,DataState from Form where DepartmentId=@DepartmentId ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<Form>(sql, new { DepartmentId = departmentId }).ToList();
        }

    }
}
